package com.hz.ws;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.hz.base.beans.vo.CommonResult;
import com.hz.beans.code.HzOperateCode;
import com.hz.common.beans.constant.DemoResult;
import com.hz.common.beans.constant.OperateCode;
import com.hz.common.beans.constant.ResultCode;
import com.hz.utils.SMSUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "短信管理",description = "operation about SMS")
@RestController
@RequestMapping("/SMS")
public class SMSController {
	@ApiOperation(value="发送短信", notes="根据信息发送短信")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "phoneNumber", value = "待发送手机号。", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "signName", value = "短信签名-可在短信控制台中找到", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "templateCode", value = "短信模板-可在短信控制台中找到", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "templateParam", value = "模板中的变量替换JSON串如{\"name\":\"Jack\"}", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "smsUpExtendCode", value = "上行短信扩展码(扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段)", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "outId", value = "提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者", required = false, dataType = "String", paramType = "query")
	})
	@RequestMapping(value = "/sendSMS" ,method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult<Object> sendSMS(String phoneNumber,String signName,String templateCode,
    		String templateParam,String smsUpExtendCode,String outId) {
		CommonResult<Object> cr = new DemoResult<>();
		cr.setOperate(HzOperateCode.SEND_SMS);
		cr.setResult(ResultCode.SEND_SMS_FALSE);
		SendSmsRequest request = new SendSmsRequest();
		System.out.println(templateParam);
		request.setPhoneNumbers(phoneNumber);
		request.setSignName(signName);
		request.setTemplateCode(templateCode);
		if(templateParam!=null)request.setTemplateParam(templateParam);
		if(smsUpExtendCode!=null)request.setSmsUpExtendCode(smsUpExtendCode);
		if(outId!=null)request.setOutId(outId);
		SendSmsResponse sendSmsResponse = SMSUtil.send(request, "LTAII5NJe42jU539", "a997Vu79E0n1i8k0kBJqMrum5UHdYW");
		if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
			cr.setResult(ResultCode.SEND_SMS_SUCCESS);
		}
		cr.setData(sendSmsResponse);
		return cr;
	}
	
	@ApiOperation(value="发送短信", notes="根据信息发送短信")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "SpCode", value = "企业编号（可不填，有默认）", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "LoginName", value = "用户名称（可不填，有默认）", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "Password", value = "用户密码（可不填，有默认）", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "MessageContent", value = "短信内容，最大402个字或字符（若不填写则为默认）", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "UserNumber", value = "手机号码（多个用,分割），最多1000个号码", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "SerialNumber", value = "流水号（默认为空字符串）", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "ScheduleTime", value = "预约发送时间，格式yyyyMMddHHmmss，如20180713151710", required = false, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "f1", value = "提交检测方式，为1时有无效号码，其余有效号码正常发出，不为1时只要有无效号码短信都不发出（默认为1）", required = false, dataType = "String", paramType = "query")
	})
	@RequestMapping(value = "/sendSMSLT" ,method = {RequestMethod.POST,RequestMethod.GET})
	public CommonResult<Object> sendSMSLT(String SpCode,String LoginName,
			String Password,String MessageContent,String UserNumber,
			String SerialNumber,String ScheduleTime,String f1) {
		CommonResult<Object> cr = new DemoResult<>();
		cr.setOperate(HzOperateCode.SEND_SMS_LT);
		cr.setResult(ResultCode.SEND_SMS_FALSE);
		String result = SMSUtil.sendLT(SpCode, LoginName, Password, MessageContent, UserNumber, SerialNumber, ScheduleTime, f1);
		if(result.contains("result=0")) {
			cr.setResult(ResultCode.SEND_SMS_SUCCESS);
		}
		cr.setData(result);
		return cr;
	}
}
